package com.milorad.ads.pages;

import java.util.Date;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.milorad.ads.data.dao.interfaces.AdvertDao;
import com.milorad.ads.data.dao.interfaces.CategoryDao;
import com.milorad.ads.data.dao.interfaces.CityDao;
import com.milorad.ads.data.domain.Advert;
import com.milorad.ads.data.domain.Advert.AdvertStatus;
import com.milorad.ads.data.domain.Category;
import com.milorad.ads.data.domain.City;

public class CreateAdvertPage extends BasePage {
	
	@SpringBean
	private AdvertDao advertDao;
	@SpringBean
	private CategoryDao categoryDao;
	@SpringBean
	private CityDao cityDao;
	
	public CreateAdvertPage() {				
		
		Form<Advert> form = new Form<Advert>("addAdvertForm", new CompoundPropertyModel<Advert>(new Advert())) {
			private static final long serialVersionUID = -4344919260680439226L;

			@Override
			protected void onSubmit() {				
				
				Advert advert = getModelObject();
				advert.setStatus(AdvertStatus.PENDING);
				advert.setDate(new Date());
				
				advertDao.save(advert);
				
				setResponsePage(CreateAdvertConfirmationPage.class);
			}			
		};
		FormComponent<Category> categoryDropDown = new DropDownChoice<Category>("category", categoryDao.findAll()).setRequired(true);
		form.add(categoryDropDown);
		FormComponent<City> cityDropDown = new DropDownChoice<City>("city", cityDao.findAll()).setRequired(true);
		form.add(cityDropDown);
		form.add(new TextField<Advert>("title").setRequired(true));
		form.add(new TextArea<Advert>("text").setRequired(true));
		
		add(form);	
		
		add(new FeedbackPanel("feedback"));
		
		add (new BookmarkablePageLink<Void>("homePageLink", AdvertsOverviewPage.class));
	}
}